// created by noellynch
// Jun 3, 2009

package com.sceneworker.monitorpopup.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.jme.math.Vector3f;
import com.jme.renderer.AbstractCamera;
import com.jme.renderer.Camera;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.cacheobjects.RenderToTextureCacheObject;
import com.sceneworker.util.swing.Vector3fEditorPanelStandAlone;

public class ModifyRTTCameraPanel extends JPanel {

	private		Vector3fEditorPanelStandAlone		mcl_camLoc;
	private		Vector3fEditorPanelStandAlone		mcl_camLookAtPos;
	private		Vector3fEditorPanelStandAlone		mcl_camLookAtUpAxis;
	private		JTextField							mcl_width;
	private		JTextField							mcl_height;
	private		Camera								mcl_cam;
	
	public	ModifyRTTCameraPanel(final RenderToTextureCacheObject cl_render, final JDialog cl_dlg) {
		setLayout(new MigLayout("inset 5"));
		
		mcl_cam = cl_render.mcl_rtt.getCamera();
		
		MigLayoutUtils.makeLabelAndSeperator(this, "Camera Location");
		mcl_camLoc = new Vector3fEditorPanelStandAlone(mcl_cam.getLocation().clone());
		add(mcl_camLoc, "wrap");
		
		MigLayoutUtils.makeLabelAndSeperator(this, "LookAt (Pos)");
		mcl_camLookAtPos = new Vector3fEditorPanelStandAlone(new Vector3f());
		add(mcl_camLookAtPos, "wrap");
		
		MigLayoutUtils.makeLabelAndSeperator(this, "LookAt (UpAxis)");
		mcl_camLookAtUpAxis = new Vector3fEditorPanelStandAlone(mcl_cam.getUp());
		add(mcl_camLookAtUpAxis, "wrap");
		
		JButton		lcl_apply = SwingComponentFactory.makeAButton("Apply");
		lcl_apply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				applyCamChanges();
			}
		});
		
		JButton		lcl_cancel = SwingComponentFactory.makeAButton("Done");
		lcl_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_dlg.setVisible(false);
			}
		});
		
		if(mcl_cam instanceof AbstractCamera) {
			MigLayoutUtils.makeLabelAndSeperator(this, "Display Width and Height");
			mcl_width = MigLayoutUtils.numberText(this, "Width", ((AbstractCamera)mcl_cam).getWidth(), true);
			mcl_height = MigLayoutUtils.numberText(this, "Height", ((AbstractCamera)mcl_cam).getHeight(), true);
		}
		
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_apply, lcl_cancel);
	}
	
	private	void	applyCamChanges() {
		if(mcl_width != null) {
			mcl_cam.resize(Integer.parseInt(mcl_width.getText()), Integer.parseInt(mcl_height.getText()));
		}
		
		mcl_cam.setLocation(mcl_camLoc.getVector());
		mcl_cam.lookAt(mcl_camLookAtPos.getVector(), mcl_camLookAtUpAxis.getVector());
		mcl_cam.update();
	}
}
